<?php
  class tree{    protected $model;
    protected $db;
    protected $view;
    protected $config;

    function __construct(&$db,&$view,&$model,&$config){
      $this->db=&$db;
      $this->view=&$view;
      $this->model=&$model;
      $this->config=&$config;
    }
    function build_tree($arg="",$arg1=""){
     $tmp=$this->model->return_cat($arg);
	 $tmp=$this->build_cat($tmp);
     return $tmp;
    }

    function build_cat($arg=""){
       		foreach($arg as $key=>$value){
                if($value['level'] !=0 && $tmp !=0){
                 if ($value['level']>$tmp){
                $ret.=$this->view->view("templates/admin/dir/div_hid.tpl",$value,true);
                	$flag=false;
                  }else{
                   if ($value['level']<$tmp)
                     for($ii=$value['level'];$ii<$tmp;$ii++){
                      $ret.=$this->view->view("templates/admin/dir/div_end.tpl","",true);
					  }
                	}
                }
            if($flag) $ret.=$this->view->view("templates/admin/dir/br.tpl","",true);
            if($value['show_me']){
            	$value['show_me']=$this->view->view("templates/admin/dir/show_image_true.tpl","",true);
            	}else{
            		$value['show_me']=$this->view->view("templates/admin/dir/show_image_false.tpl","",true);
            		}
            $ret.=$this->view->view("templates/admin/dir/menu.tpl",$value,true);
            $value['space']=str_repeat("&nbsp;",$value['level']);
            $ret.=$this->view->view("templates/admin/dir/link.tpl",$value,true);
            $flag=true;
            $tmp=$value['level'];
         	}

         	for($ii=1;$ii<$tmp;$ii++){
             $ret.=$this->view->view("templates/admin/dir/div_end.tpl","",true);
			}
            return $ret;
    }

    function get_doc($id,$pag=1,$id_doc="",$path=""){
    $pag_tmp=$pag;
    $pag=($pag*5)-5;
    $sm=SUM_PAGE;
    $tmp=$this->db->result_array($this->db->query("SELECT SQL_CALC_FOUND_ROWS * From ns_doc WHERE father_id='$id' and module ='$module' ORDER BY date DESC LIMIT $pag,$sm  ;"));
    $total=$this->model->get_info('',"FOUND_ROWS()","SELECT FOUND_ROWS();");

    if (!count(($tmp)))return $this->view->view("templates/admin/table/empty_dir.tpl","",true);
    // create dir
       foreach ($tmp as $key=>$value){
		if($value['show_me']==1)$value['show_me']=$this->view->view("templates/admin/table/table_red.tpl","",true);
			else
			  $value['show_me']=$this->view->view("templates/admin/table/table_red_false.tpl","",true);
        $value['pag']= $pag_tmp;
        $value['cat_id']=$id;
        $ret['dir'].=$this->view->view("templates/admin/table/table_d.tpl",$value,true);
       }
       //pagination
        $page= ceil(($total)/5);
        for($i=1;$i<=$page;$i++){
        	if($id_doc==""){
             $data['link']=$path."/".$id."/".$i;
        		}else{
                 $data['link']="edit_doc/".$id_doc."/".$i."/".$id;
        		};
        $data['num']=$i;
           if($i==$pag_tmp){
           	$ret['pagination'].=$this->view->view("templates/admin/table/pagination2.tpl",$data,true);
           	}else{
        	 $ret['pagination'].=$this->view->view("templates/admin/table/pagination.tpl",$data,true);
        	}
        }
    $ret=$this->view->view("templates/admin/table/table.tpl",$ret,true);
    return $ret;
    }

    function get_dir($id){
     $tmp=$this->db->result_array($this->db->query("SELECT name,id From ns_tree ORDER BY lft "));
     $value['name']=$this->model->get_info($id,"name");
     $value['id']=$id;
     $ret.=$this->view->view("templates/admin/doc/option.tpl",$value,true);
     foreach($tmp as $key=>$value){
     	$ret.=$this->view->view("templates/admin/doc/option.tpl",$value,true);
     	}


     return $ret;
    }

     function return_menu($p_id,$side=""){
      $tmp=$this->model->return_menu($p_id,$side,$_SESSION['lang']);

     foreach($tmp as $key=>$value){     	$data['name']=$value['text'];
     	if ($p_id==$value['name']){     	 if($value['templates']=="vert"){
           $templetas1="templates/site/tpl_module/tree/link_vertical/link_vert_!active.tpl";
          }else{
      	    $templetas1="templates/site/tpl_module/tree/link_horzontal/link_hor_!active.tpl";
     	 }
     	 }else{     	  if($value['templates']=="vert"){
           $templetas1="templates/site/tpl_module/tree/link_vertical/link_vert_active.tpl";
           }else{
      	    $templetas1="templates/site/tpl_module/tree/link_horzontal/link_hor_active.tpl";
     	   }
     	   	if ($value['adress']){
     	 $data['operator']=$value['adress'];
     	 }else{
     	  if($value['func_name']){
			$data['operator']=$value['module']."/".$value['func_name']."/".$value['name'];
           }else{
          	$data['operator']=$value['module'];
           }
     	 }
     	 }



     	$data['space']=str_repeat("&nbsp;",$value['level']);

      $ret.=$this->view->view($templetas1,$data,true);

     }
     return $ret;
    }

    function return_doc($id,$module){     $tmp=$this->model->return_doc($id,$module);
     foreach($tmp as $key=>$value){      echo $value['name'];
     }
     return $tmp;
    }
  }
?>